package com.sailorj.itools.toolWindow;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogBuilder;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentManager;
import com.sailorj.itools.entity.ParserColumn;
import com.sailorj.itools.entity.ParserTable;
import com.sailorj.itools.generate.DDLParser;
import com.sailorj.itools.gui.ConfigForm;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.util.TextUtils;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

public class MyToolWindowFactory implements ToolWindowFactory {
    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        try {
            // 构建对话框
            DialogBuilder dialogBuilder = new DialogBuilder(project);
            // 创建GUI对象 设置对话框显示内容
            dialogBuilder.setCenterPanel(ConfigForm.getConfigForm());
            ConfigForm.setKeyName("请输入DDL");
            ConfigForm.setKeyFromName("不填");
            dialogBuilder.setTitle("根据DDL SQL生成showDoc文档");
            dialogBuilder.setOkOperation(() -> {
                String sql = ConfigForm.getTextFieldApiKey().getText();
                if (!TextUtils.isEmpty(sql)) {
                    ParserTable parserTable = DDLParser.parseSql(sql);
                    String content = "", tableName = "";

                    /**** 开始生成 ****/
                    if (!StringUtils.isAllBlank(parserTable.getTableComment())) {
                        tableName = tableName + "(" + parserTable.getTableComment() + ")";
                    }
                    content += "\n";
                    content += "\n" + " - " + tableName;
                    content += "\n";
                    content += "\n" + "|字段|类型|空|默认|注释|";
                    content += "\n" + "|:----    |:-------    |:--- |-- -|------      |";
                    for (ParserColumn parserColumn : parserTable.getColumnList()) {
                        /**** 开始生成 ****/
                        content += "\n" + "|" + parserColumn.getColumnName() + "|" + parserColumn.getColumnTypes() + "|"
                                + parserColumn.getIsNull() + "|" + parserColumn.getDefaultValue() + "|" + parserColumn.getColumnComment();
                    }

                    // 此处方法将会在点击ToolWindow的时候触发
                    // 获取ContentManager
                    ContentManager contentManager = toolWindow.getContentManager();
                    Content labelContent =
                            contentManager.getFactory() // 内容管理器获取工厂类
                                    .createContent( // 创建Content（组件类实例、显示名称、是否可以锁定）
                                            new JLabel(content),
                                            "根据DDL SQL生成showDoc文档",
                                            false
                                    );
                    // 利用ContentManager添加Content
                    contentManager.addContent(labelContent);
                }
                // 关闭对话框
                dialogBuilder.getDialogWrapper().close(0);
            });
            // 显示对话框
            dialogBuilder.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
